package com.runshu.rest.converter;


import com.runshu.utils.JsonUtils;

/**
 * Created by DON on 2015/7/6.
 */
public class SimpleBeanConverter implements BeanConverter {

    @Override
    @SuppressWarnings("unchecked")
    public <T> T converter(String body, Class<?> type) {
        if(type == String.class){
            return (T)body;
        }else if (type == Boolean.TYPE || type == Boolean.class) {        // boolean
            return (T)Boolean.valueOf(body);
        } else if (type == Character.TYPE || type == Character.class) { // char
            return (T)Character.valueOf(body.toCharArray()[0]);
        } else if (type == Byte.TYPE || type == Byte.class) {    // byte
            return (T)Byte.valueOf(body);
        } else if (type == Short.TYPE || type == Short.class) {   // short
            return (T)Short.valueOf(body);
        } else if (type == Integer.TYPE || type == Integer.class) { // int
            return (T)Integer.valueOf(body);
        } else if (type == Long.TYPE || type == Long.class) {    // long
            return (T)Long.valueOf(body);
        } else if (type == Float.TYPE || type == Float.class) {   // float
            return (T)Float.valueOf(body);
        } else if (type == Double.TYPE || type == Double.class) {  // double
            return (T)Double.valueOf(body);
        } else if (type == Void.TYPE || type == Void.class) {    // void
            return null;
        }
        return (T) JsonUtils.toObjectIgnoreAnn(body, type);
    }
}
